package com.drug.wms.service.impl;

import com.drug.common.enums.BusinessSeqEnum;
import com.drug.common.utils.uuid.IdUtils;
import com.drug.system.service.ISysDictDataService;
import com.drug.wms.domain.*;
import com.drug.wms.mapper.BusFileMapper;
import com.drug.wms.mapper.BusGoodsBaseInfoMapper;
import com.drug.wms.mapper.BusGoodsPriceRelationMapper;
import com.drug.wms.page.BusGoodsBaseInfoAndSkuPage;
import com.drug.wms.page.BusGoodsBaseInfoPage;
import com.drug.wms.service.GenerateSequence;
import com.drug.wms.service.IBusGoodsBaseInfoService;
import com.drug.wms.service.IBusManufacturerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品基本信息Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-09
 */
@Service
public class BusGoodsBaseInfoServiceImpl implements IBusGoodsBaseInfoService {
    @Autowired
    private BusGoodsBaseInfoMapper busGoodsBaseInfoMapper;
    @Autowired
    private BusGoodsPriceRelationMapper busGoodsPriceRelationMapper;
    @Autowired
    private BusFileMapper busFileMapper;

    @Autowired
    private ISysDictDataService dictService;


    /**
     * 查询商品基本信息
     *
     * @param id 商品基本信息主键
     * @return 商品基本信息
     */
    @Override
    public BusGoodsBaseInfoPage selectBusGoodsBaseInfoById(String id) {
        BusGoodsBaseInfoPage busGoodsBaseInfoPage = new BusGoodsBaseInfoPage();
        //商品基本信息
        BusGoodsBaseInfo busGoodsBaseInfo = busGoodsBaseInfoMapper.selectBusGoodsBaseInfoById(id);
        busGoodsBaseInfoPage.setBusGoodsBaseInfo(busGoodsBaseInfo);
        //价格信息
        BusGoodsPriceRelation query1 = new BusGoodsPriceRelation();
        query1.setGoodsNo(id);
        List<BusGoodsPriceRelation> busGoodsPriceRelations = busGoodsPriceRelationMapper.selectBusGoodsPriceRelationList(query1);
        for (BusGoodsPriceRelation b : busGoodsPriceRelations) {
            b.setPriceTypeNo_dictText(dictService.queryTableDictTextByKey("bus_price_conf", "price_name", "id", b.getPriceTypeNo()));
        }

        busGoodsBaseInfoPage.setPrices(busGoodsPriceRelations);
        //上传资料信息
        BusFile query2 = new BusFile();
        query2.setBusId(id);
        busGoodsBaseInfoPage.setFiles(busFileMapper.selectBusFileList(query2));
        return busGoodsBaseInfoPage;
    }

    /**
     * 查询商品基本信息列表
     *
     * @param busGoodsBaseInfo 商品基本信息
     * @return 商品基本信息
     */
    @Override
    public List<BusGoodsBaseInfo> selectBusGoodsBaseInfoList(BusGoodsBaseInfo busGoodsBaseInfo) {
        return busGoodsBaseInfoMapper.selectBusGoodsBaseInfoList(busGoodsBaseInfo);
    }
    @Autowired
    private IBusManufacturerInfoService busManufacturerInfoService;

    /**
     * 新增商品基本信息
     *
     * @param busGoodsBaseInfoPage 商品基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertBusGoodsBaseInfo(BusGoodsBaseInfoPage busGoodsBaseInfoPage) {
        //基本信息
        BusGoodsBaseInfo busGoodsBaseInfo = busGoodsBaseInfoPage.getBusGoodsBaseInfo();
        String goodNo = GenerateSequence.genSeqNo(BusinessSeqEnum.SP.getCode());
        busGoodsBaseInfo.setGoodsNo(goodNo);
        String mid = IdUtils.fastUUID();
        busGoodsBaseInfo.setId(mid);
        //获取生产厂商的名称
        BusManufacturerInfo busManufacturerInfo = busManufacturerInfoService.selectBusManufacturerInfoById(busGoodsBaseInfo.getManufacturerNo());
        busGoodsBaseInfo.setManufacturerName(busManufacturerInfo.getName());
        busGoodsBaseInfoMapper.insertBusGoodsBaseInfo(busGoodsBaseInfo);
        BusGoodsBaseInfo query = new BusGoodsBaseInfo();
        query.setGoodsNo(goodNo);
        //价格
        List<BusGoodsPriceRelation> prices = busGoodsBaseInfoPage.getPrices();
        if (prices != null && prices.size() != 0) {
            for (BusGoodsPriceRelation b : prices) {
                b.setGoodsNo(mid);
                busGoodsPriceRelationMapper.insertBusGoodsPriceRelation(b);
            }
        }

        //上传资料
        List<BusFile> files = busGoodsBaseInfoPage.getFiles();
        if (files != null && files.size() != 0) {
            for (BusFile b : files) {
                b.setId(null);
                b.setBusId(mid);
                busFileMapper.insertBusFile(b);
            }
        }
        return 1;
    }

    /**
     * 修改商品基本信息
     *
     * @param busGoodsBaseInfoPage 商品基本信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBusGoodsBaseInfo(BusGoodsBaseInfoPage busGoodsBaseInfoPage) {
        BusGoodsBaseInfo busGoodsBaseInfo = busGoodsBaseInfoPage.getBusGoodsBaseInfo();
        //获取生产厂商的名称
        BusManufacturerInfo busManufacturerInfo = busManufacturerInfoService.selectBusManufacturerInfoById(busGoodsBaseInfo.getManufacturerNo());
        busGoodsBaseInfo.setManufacturerName(busManufacturerInfo.getName());
        busGoodsBaseInfoMapper.updateBusGoodsBaseInfo(busGoodsBaseInfo);

        //批量删除价格表数据
        busGoodsPriceRelationMapper.deleteBusGoodsPriceRelationByGoodsNo(busGoodsBaseInfo.getId());

        List<BusGoodsPriceRelation> prices = busGoodsBaseInfoPage.getPrices();
        if (prices != null && prices.size() != 0) {
            for (BusGoodsPriceRelation b : prices) {
                b.setGoodsNo(busGoodsBaseInfo.getId());
                busGoodsPriceRelationMapper.insertBusGoodsPriceRelation(b);
            }
        }

        //批量删除上传资料数据
        busFileMapper.deleteBusFileByBusId(busGoodsBaseInfo.getId());
        //上传资料
        List<BusFile> files = busGoodsBaseInfoPage.getFiles();
        if (files != null && files.size() != 0) {
            for (BusFile b : files) {
                b.setBusId(busGoodsBaseInfo.getId());
                busFileMapper.insertBusFile(b);
            }
        }
        return 1;
    }

    /**
     * 批量删除商品基本信息
     *
     * @param ids 需要删除的商品基本信息主键
     * @return 结果
     */
    @Override
    public int deleteBusGoodsBaseInfoByIds(String[] ids) {
        return busGoodsBaseInfoMapper.deleteBusGoodsBaseInfoByIds(ids);
    }

    /**
     * 删除商品基本信息信息
     *
     * @param id 商品基本信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteBusGoodsBaseInfoById(String id) {
        busGoodsPriceRelationMapper.deleteBusGoodsPriceRelationByGoodsNo(id);
        busFileMapper.deleteBusFileByBusId(id);
        return busGoodsBaseInfoMapper.deleteBusGoodsBaseInfoById(id);
    }

    @Override
    public List<BusGoodsBaseInfoAndSku> getSaleGoods(BusGoodsBaseInfoAndSkuPage busGoodsBaseInfoAndSkuPage) {
        return busGoodsBaseInfoMapper.getSaleGoods(busGoodsBaseInfoAndSkuPage);
    }
}
